﻿using System.Linq;
using System.Text;
using System.Collections.Generic;
using Dapper;
using Eagles.Base.DataBase;
using Eagles.Application.Model.Common;
using Eagles.Interface.DataAccess.Util;
using Eagles.DomainService.Model.Org;
using Eagles.DomainService.Model.User;
using Eagles.DomainService.Model.RewardScore;

namespace Ealges.DomianService.DataAccess.Util
{
    public class Util : IUtil
    {
        private readonly IDbManager dbManager;

        public Util(IDbManager dbManager)
        {
            this.dbManager = dbManager;
        }

        public int CreateUserNotice(TbUserNotice userNotice)
        {
            //增加用户通知
            return dbManager.Excuted(@"insert into eagles.tb_user_notice(OrgId,NewsType,Title,UserId,Content,IsRead,FromUser,CreateTime,TargetUrl)
values (@OrgId,@NewsType,@Title,@UserId,@Content,@IsRead,@FromUser,@CreateTime,@TargetUrl); ", userNotice);
        }

        public int CreateScoreLs(TbUserScoreTrace userScoreTrace)
        {
            //增加修改积分的流水
            return dbManager.Excuted(@"insert into eagles.tb_user_score_trace (OrgId,UserId,TraceId,CreateTime,Score,RewardsType,Comment,OriScore,Source)
values (@OrgId,@UserId,@TraceId,@CreateTime,@Score,@RewardsType,@Comment,@OriScore,@Source); ", userScoreTrace);
        }

        public int EditUserScore(int userId, int score)
        {
            //修改用户积分
            return dbManager.Excuted(@"update eagles.tb_user_info set Score = ifnull(Score,0) + @Score where UserId = @UserId ", new { UserId = userId, Score = score });
        }

        public bool BatchEditUserScore(List<JoinPeople> userList, int score)
        {
            if (userList.Count <= 0)
                return false;
            var sql = new StringBuilder();
            var parameters = new DynamicParameters();
            sql.Append("update eagles.tb_user_info set Score = ifnull(Score,0) + @Score where UserId in @UserId ");
            List<int> user = new List<int>();
            userList.ForEach(x => user.Add(x.UserId));
            parameters.Add("UserId", user);
            parameters.Add("Score", score);
            dbManager.Excuted(sql.ToString(), parameters);
            return true;
        }

        public bool CheckAppId(int appId)
        {
            var user = dbManager.Query<TbOrgInfo>(@" select OrgId, OrgName, Province, City, District, Address, CreateTime, EditTime, OperId, Logo from eagles.tb_org_info where OrgId = @OrgId", new { OrgId = appId });
            if (user != null && user.Any())
                return true;
            return false;
        }

        public TbRewardScore RewardScore(int orgId, string rewardType)
        {
            //查询任务奖励积分
            return dbManager.QuerySingle<TbRewardScore>("select Score,LearnTime,KeyWord,WordCount from eagles.tb_reward_score where OrgId = @OrgId and RewardType = @RewardType ", new { OrgId = orgId, RewardType = rewardType });
        }

        public TbUserToken GetUserId(string token, int tokenType)
        {
            var tokens = dbManager.Query<TbUserToken>(@"select b.OrgId,b.BranchId,a.UserId,a.Token,a.CreateTime,a.ExpireTime,a.TokenType from eagles.tb_user_token a
inner join eagles.tb_user_info b on a.UserId = b.UserId where a.Token = @Token and a.TokenType = @TokenType and IsOut = @IsOut ", new { Token = token, TokenType = tokenType, IsOut = 0 });
            if (tokens != null && tokens.Any())
                return tokens.FirstOrDefault();
            return null;
        }

        public TbUserInfo GetUserInfo(int userId)
        {
            var user = dbManager.Query<TbUserInfo>(@"select OrgId,BranchId,UserId,Name,IsLeader,Score,Phone from eagles.tb_user_info where UserId = @UserId", new { UserId = userId });
            if (user != null && user.Any())
                return user.FirstOrDefault();
            return null;
        }

        public TbUserRelationship GetSuperior(int userId)
        {
            var user = dbManager.Query<TbUserRelationship>(@"select UserId,(select Name from eagles.tb_user_info b where b.UserId=a.UserId) Name from eagles.tb_user_relationship a where SubUserId = @SubUserId", new { SubUserId = userId });
            if (user != null && user.Any())
                return user.FirstOrDefault();
            return null;
        }

        public TbOrgInfo GetOrgInfo(int orgId)
        {
            var user = dbManager.Query<TbOrgInfo>(@"select OrgId,OrgName,Secretary,ViceSecretary,ScoreType from eagles.tb_org_info where OrgId = @OrgId", new { OrgId = orgId });
            if (user != null && user.Any())
                return user.FirstOrDefault();
            return null;
        }
    }
}